<!--
 * @Description: 结算单打印
 * @Autor: 何兴家
 * @Date: 2021-02-03 16:59:17
 * @LastEditors: 马毅
 * @LastEditTime: 2021-07-27 16:25:41
-->
<template>
  <div style="margin-right: 10px; margin-right: 10px; display: inline-block">
    <AjaxButton type="primary" :disabled="disabled" :click="handlePrint">{{ btnText }}</AjaxButton>

    <BasePrint ref="print" :showTopIcon="true" :title="printTitle" :data="printData" :isPreview="false" template="repair/ballanceOrder" />
  </div>
</template>

<script>
import _ from 'lodash';
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, findFormItem, findTableItem } from '@/utils';
import { getAppId } from '@/assets/js/auth';

import { printSettlementRepairOrder } from '@/api/repair/repairReception/feesSettlement';

export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    btnText: {
      type: String,
      default: '打印'
    },
    roNo: {
      type: String,
      required: true,
      default: ''
    },
    printType: {
      // 打印类型(正式打印 formal 预打印 pre)
      type: String,
      default: 'formal'
    },
    defaultData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  mixins: [dictionary],
  data() {
    return {
      printData: {}
    };
  },
  computed: {
    printTitle() {
      return getAppId() == 'beijing' ? '北京汽车特约经销店维修结算单' : '蓝谷极狐授权服务维修结算单';
    }
  },
  methods: {
    // 点击打印按钮
    async handlePrint() {
      let res = await printSettlementRepairOrder({ roNo: this.roNo, isPredictPrint: this.printType == 'pre' ? 'pre' : '' });
      if (res.resultCode == 200) {
        let printData = res.data;
        // 预打印手动拼接页面数据
        if (this.printType == 'pre') {
          let preData = _.cloneDeep(this.defaultData);
          preData.ARREARS_AMOUNT = preData.arrBalance == 1 ? preData.RECEIVE_AMOUNT : 0;
          preData.arrBalance == 1 && (preData.RECEIVE_AMOUNT = 0);
          preData.labourList.forEach(item => {
            item.CHARGE_PARTITION_NAME = preData.cahrgePartitionDicts.find(item2 => item2.value == item.CHARGE_PARTITION_CODE)
              ? preData.cahrgePartitionDicts.find(item2 => item2.value == item.CHARGE_PARTITION_CODE).text
              : '';
          });
          preData.partList.forEach(item => {
            item.CHARGE_PARTITION_NAME = preData.cahrgePartitionDicts.find(item2 => item2.value == item.CHARGE_PARTITION_CODE)
              ? preData.cahrgePartitionDicts.find(item2 => item2.value == item.CHARGE_PARTITION_CODE).text
              : '';
          });
          printData = { ...printData, ...preData };
        }
        this.printData = printData;
        this.$refs.print.EXCUTE_PRINT();
      }
    }
  }
};
</script>

<style scoped></style>
